/*对getDate函数进行重载，timestamp为可省参数*/
function getDate(type: 'string', timestamp?: string): string;
function getDate(type: 'date', timestamp?: string): Date;
function getDate(type: 'string' | 'date', timestamp?: string): Date | string {
    const date = new Date(timestamp);
    return type === 'string' ? date.toLocaleString() : date;
}

const x = getDate('date');  //x:Date
const y = getDate('string', '2018-01-10');  //y:string


interface IGetDate {
    // (type: 'string', timestamp?: string): string;
    (type: 'string', timestamp?: string): any;

    // (type: 'date', timestamp?: string): Date;
    (type: 'date', timestamp?: string): any;

    (type: 'string' | 'date', timestamp?: string): Date | string;
}

/*不能将类型“（type:any,timestamp:any）=>string|Date”分配给类型IGetDate。
* 不能将类型“string|Date”分配给类型“string”。
* 不能将类型“Date”分配给类型“string”。ts（2322）错误*/
/*const getDate2: IGetDate = (type, timestamp) => {
    const date = new Date(timestamp);
    return type === 'string' ? date.toLocaleString() : date;
}*/
const getDate2: IGetDate = (type, timestamp) => {
    const date = new Date(timestamp);
    return type === 'string' ? date.toLocaleString() : date;
}
